// 如果检测到没有 token，则直接跳回登录页面
// TODO: 需要修改
import { Context } from '@nuxt/types'
import { CustomCookie } from '~/utils/cookes'
export default function ({ store, route, req, res, redirect }: Context) {
  let userId
  if (process.server) {
    const cookies = CustomCookie.getcookiesInServer(req)
    userId = cookies.cookie_user_id ? cookies.cookie_user_id : ''
  }

  if (process.client) {
    userId = CustomCookie.getCookie('cookie_user_id')
  }

  if (userId) {
    store.commit('setUserId', userId)
    return store.dispatch('getUserInfo')
  }
  return redirect('/login')
}
